package utils.file;

import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import utils.system.SystemUtils;

/**
 *
 * @author Juan Carlos García
 */
public class FileUtils {
    
    /**
     * This method saves the current File with the final content
     * 
     * @param route The route to save the file
     * @param title The title of the new file
     * @param content The content of the new File
     * @param fileTree The JTree to refresh
     */
    public static void saveFile(String route,String title,String content,JTree fileTree){
        
        FileWriter file = null;
        PrintWriter pw = null;
        try
        {
            String finalRoute = route+"/"+title;
            file = new FileWriter(finalRoute);
            pw = new PrintWriter(file);

            pw.print(content);
            
            file.close();
            
            JOptionPane.showMessageDialog(null, "File saved correctly");
            
            SystemUtils.refreshTree(fileTree,route); //JTree refresh

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
    }
    
}
